package itsm.isperp.framework.data.excel.validate;

import java.util.Map;

import org.apache.commons.collections.MapUtils;

/**
 * 长度校验器
 */
public class LengthValidator extends AbstractValidator {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * itsm.isperp.framework.data.excel.validate.AbstractValidator#processValidate
	 * (java.util.Map)
	 */
	@Override
	public String processValidate(Map<String, Object> params) {
		if (MapUtils.isEmpty(params)) {
			return "长度验证器上的参数有误！";
		}
		Object value = getFieldValue();
		if (value == null) {
			return OK;
		}

		Object maxLengthStr = params.get("maxLength");
		Object minLengthStr = params.get("minLength");
		if (maxLengthStr == null || minLengthStr == null) {
			return "长度验证器上的参数有误！";
		}

		int maxLength = Integer.parseInt(maxLengthStr.toString());
		int minLength = Integer.parseInt(minLengthStr.toString());

		if (value.toString().length() >= minLength
				&& value.toString().length() <= maxLength) {
			return OK;
		}

		return getCellRef() + "单元格数据 : " + getFieldValue() + ", 长度不合法, 必须在 "
				+ minLength + "~" + maxLength + " 之间!";
	}
}